
cap log close
log using "$logs/an-ocr-sensitivity-models.log", replace
	
	use "$data/1216_countylevel_clean.dta", clear
	
/********************************************************************************
	Merge data
********************************************************************/
	
	// Merge with counties that have SEDA Gap data
	merge 1:1 countyid using "$data/sedagaps", keep(3) nogen
	
	// Merge with sample 
	merge 1:1 countyid using "$data/commonsamp" 
	tab _merge // 78% of counties
	keep if _merge == 3
	drop _merge
	
	// Merge with covariates
	merge 1:1 countyid using "$data/seda_covariates_clean", assert(2 3) nogen keep(3) 
	
	// Merge with bias
	merge 1:1 countyid using "$data/mrp_data", assert(2 3) nogen keep(3)

// Prep data for analysis

	tostring countyid, gen(state)
	assert length(state)==4|length(state)==5
	gen temp = substr(state,1,1) if length(state)==4
	replace temp = substr(state,1,2) if length(state)==5
	drop state
	rename temp state
	destring state, replace
	tab state, gen(state_)
	
	gen origtchimpbias = tch_imp_bias
	gen origtchexpbias = tch_exp_bias
	foreach var in tch_imp_bi all_imp_bias all_exp_bias tch_imp_bias tch_exp_bias {
		egen temp = std(`var')
		replace `var' = temp
		drop temp
		}
		
		foreach var in iss oos {
			foreach r in bl wh {
				gen pr_`var'_`r' = `var'_`r'_all / enr_`r'_all
				}
				gen diff_pr_`var' = pr_`var'_bl-pr_`var'_wh
				}
		sum diff_*
		
/********************************************************************************
	County-level suspension risk differences	
********************************************************************/
		
	// Gen risk differences
	expand 2
	bys countyid: gen susp=_n-1
	
	foreach var in iss oos { 
		gen `var'bltemp = `var'_bl_all if susp==1
			replace `var'bltemp = enr_bl_all-`var'_bl_all if susp==0
		gen `var'whtemp = `var'_wh_all if susp==1
			replace `var'whtemp = enr_wh_all-`var'_wh_all if susp==0
			}
		reg susp i.countyid [aw=issbltemp] 
		predict issbl, xb
		predict issblse, stdp
		reg susp i.countyid [aw=isswhtemp] 
		predict isswh, xb
		predict isswhse, stdp
		reg susp i.countyid [aw=oosbltemp] 
		predict oosbl, xb
		predict oosblse, stdp
		reg susp i.countyid [aw=ooswhtemp] 
		predict ooswh, xb
		predict ooswhse, stdp
		
		egen tag = tag(countyid)

		foreach var in iss oos {
			gen `var'diff = `var'bl-`var'wh 
			gen s = ((enr_bl_all-1)*`var'blse^2)+((enr_wh_all-1)*`var'whse^2)
			replace s = s/(enr_bl_all+enr_wh_all-2)
			replace s = sqrt(s)
			gen `var'diffse=sqrt((1/enr_bl_all)+(1/enr_wh_all))
			replace `var'diffse=s*`var'diffse
			drop s
			}
		
	est clear 
	
	eststo: metareg issdiff all_imp_bias state_* if tag, wsse(issdiffse) mm
	eststo:metareg issdiff all_imp_bias ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* if tag, wsse(issdiffse) mm
	eststo: metareg oosdiff all_imp_bias state_* if tag, wsse(oosdiffse) mm
	eststo: metareg oosdiff all_imp_bias ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* if tag, wsse(oosdiffse) mm	
	eststo: metareg issdiff all_exp_bias state_* if tag, wsse(issdiffse) mm
	eststo:metareg issdiff all_exp_bias ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* if tag, wsse(issdiffse) mm
	eststo: metareg oosdiff all_exp_bias state_* if tag, wsse(oosdiffse) mm
	eststo: metareg oosdiff all_exp_bias ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* if tag, wsse(oosdiffse) mm			
		
		preserve
			keep if tchsamp
			
			eststo: metareg issdiff tch_imp_bias state_* if tag, wsse(issdiffse) mm
			eststo: metareg issdiff tch_imp_bias ///
				sesall ///
				perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
				ppexp_inst stutch_all ///
				flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
				state_* if tag, wsse(issdiffse) mm
			eststo: metareg oosdiff tch_exp_bias state_* if tag, wsse(oosdiffse) mm
			eststo: metareg oosdiff tch_exp_bias ///
				sesall ///
				perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
				ppexp_inst stutch_all ///
				flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
				state_* if tag, wsse(oosdiffse) mm	
		restore
		
/********************************************************************************
	Fractional response model
********************************************************************/			
	
	gen priss = .
	gen proos = .
	
	rename susp black
	
	foreach var in iss oos {
		replace pr`var' = pr_`var'_bl if black == 1
		replace pr`var' = pr_`var'_wh if black == 0
		}
	gen enr = .
		replace enr=enr_bl_all if black == 1
		replace enr=enr_wh_all if black==0
		
	eststo: fracreg probit priss black all_imp_bias c.all_imp_bias#black state_* [fw=enr], vce(cluster countyid)
	eststo: fracreg probit priss black all_imp_bias c.all_imp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* [fw=enr], vce(cluster countyid)
	eststo: fracreg probit proos black all_imp_bias c.all_imp_bias#black state_* [fw=enr], vce(cluster countyid)
	eststo: fracreg probit proos black all_imp_bias c.all_imp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* [fw=enr], vce(cluster countyid)		
		
	eststo: fracreg probit priss black all_exp_bias c.all_exp_bias#black state_* [fw=enr], vce(cluster countyid)
	eststo: fracreg probit priss black all_exp_bias c.all_exp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* [fw=enr], vce(cluster countyid)
	eststo: fracreg probit proos black all_exp_bias c.all_exp_bias#black state_* [fw=enr], vce(cluster countyid)
	eststo: fracreg probit proos black all_exp_bias c.all_exp_bias#black ///
		sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
		state_* [fw=enr], vce(cluster countyid)		
		
		preserve
			keep if tchsamp
			
			eststo: fracreg probit priss black tch_imp_bias c.tch_imp_bias#black state_* [fw=enr], vce(cluster countyid)
			eststo: fracreg probit priss black tch_imp_bias c.tch_imp_bias#black ///
				sesall ///
				perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
				ppexp_inst stutch_all ///
				flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
				state_* [fw=enr], vce(cluster countyid)
			eststo: fracreg probit proos black tch_imp_bias c.tch_imp_bias#black state_* [fw=enr], vce(cluster countyid)
			eststo: fracreg probit proos black tch_imp_bias c.tch_imp_bias#black ///
				sesall ///
				perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
				ppexp_inst stutch_all ///
				flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
				state_* [fw=enr], vce(cluster countyid)		
				
			eststo: fracreg probit priss black tch_exp_bias c.tch_exp_bias#black state_* [fw=enr], vce(cluster countyid)
			eststo: fracreg probit priss black tch_exp_bias c.tch_exp_bias#black ///
				sesall ///
				perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
				ppexp_inst stutch_all ///
				flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
				state_* [fw=enr], vce(cluster countyid)
			eststo: fracreg probit proos black tch_exp_bias c.tch_exp_bias#black state_* [fw=enr], vce(cluster countyid)
			eststo: fracreg probit proos black tch_exp_bias c.tch_exp_bias#black ///
				sesall ///
				perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
				ppexp_inst stutch_all ///
				flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk ///
				state_* [fw=enr], vce(cluster countyid)		

		restore

		esttab using "$output/ocr-sensitivity-models.csv", se r2 ar2 drop(state_* sesall ///
		perblk perhsp /*pctenglish1*/ hswhtblk hsflnfl percharter_all ///
		ppexp_inst stutch_all ///
		flunchwhtblk percharterwhtblk stutchwhtblk seswhtblk) replace note(includes state FE) ///
		star(~ 0.10 * 0.05 ** 0.01 *** 0.001)
	
log close
